<?php
/**
 * Created by PhpStorm.
 * User: guo
 * Date: 2017/6/17
 * Time: 15:35
 */
namespace core\lib;
class route
{
    public $controller;
    public $action;
    public function __construct()
    {
        /**
         * 1、隐藏index.php        (此处用的nginx的，如果使用apache可以通过.htaccess进行设置)
         * 2、获取当中的参数部分
         * 3、返回控制器和方法
         */
        if(isset($_SERVER['REQUEST_URI']) && $_SERVER['REQUEST_URI'] != '/')
        {
            $path    = $_SERVER['REQUEST_URI'];
            $patharr = explode('/',trim($path,'/'));
            if(isset($patharr[0]))
            {
                $this->controller = $patharr[0];
                unset($patharr[0]);
            }else
            {
                $this->controller = 'index';
            }
            if(isset($patharr[1]))
            {
                $this->action = $patharr[1];
                unset($patharr[1]);
            }else
            {
                $this->action = 'index';
            }
            //将url的路由部分转换为GET参数
            $count = count($patharr);
            $i = 2;
            while ($i <= $count)
            {
                if(isset($patharr[$i + 1]))             //防止数组越界（在nginx暂时未发现问题）
                {
                    $_GET[$patharr[$i]] = $patharr[$i + 1];
                }
                $i = $i + 2;
            }
        }else
        {
            $this->controller = 'index';
            $this->action     = 'index';
        }
    }
}